/*
The surface-physics project: http://code.google.com/p/surface-physics/
Adds real world physics based on the Bullet physics engine, to bases in the Orbiter space
flight simulation software.

Written by Abhijit Nandy
License : Apache License 2.0

Bump_MshCollisionShapeAttached.h

Used to draw the following shapes inside Orbiter :

1. Sphere (parameters : radius, position, orientation)
2. Cylinder (parameters : radius, length, position, orientation) - only one currently
3. Box (parameters : radius, length, position, orientation)
4. Cone
5. Capsule


It adds animations and uses Euler angles to transform the object properly

TODO: Adds colors to show state. No need of wireframe view yet.


*/


#pragma once

// Orbiter
#include "Bump_BulletBase.h"
#include "VesselCollisionDef.h"



/**
 * The Bump_CollisionShape class to create and load meshes and manage them
 * using base attached vessels
 */
class Bump_CollisionShape
{
	//The containing Bump_BulletBase
	Bump_BulletBase *bumpBase;


	// The mesh in which the collision mesh resides
	// For compound meshes multiple groups are used
	MESHHANDLE mMHCollisionMesh;

	// Independent flags for shape set, vessel attached, vessel created
	// These need to be independently tracked to free memory
	bool mShapeSet;

	// To track the number of shapes in a compound shape
	int mNumShapes;


	// Animation setup
	UINT group[VesselCollisionDef::MAX_SHAPES][1]; //  participating groups

	// Translation animation mesh groups
	MGROUP_TRANSLATE *mgtTX[VesselCollisionDef::MAX_SHAPES];
	MGROUP_TRANSLATE *mgtTY[VesselCollisionDef::MAX_SHAPES];
	MGROUP_TRANSLATE *mgtTZ[VesselCollisionDef::MAX_SHAPES];

	// Translation animation IDs
	UINT animTX[VesselCollisionDef::MAX_SHAPES];
	UINT animTY[VesselCollisionDef::MAX_SHAPES];
	UINT animTZ[VesselCollisionDef::MAX_SHAPES];

	// Rotation animation mesh groups
	MGROUP_ROTATE *mgrRX[VesselCollisionDef::MAX_SHAPES];
	MGROUP_ROTATE *mgrRY[VesselCollisionDef::MAX_SHAPES];
	MGROUP_ROTATE *mgrRZ[VesselCollisionDef::MAX_SHAPES];

	// Rotation animation IDs
	UINT animRX[VesselCollisionDef::MAX_SHAPES];
	UINT animRY[VesselCollisionDef::MAX_SHAPES];
	UINT animRZ[VesselCollisionDef::MAX_SHAPES];

	// Scaling animation mesh groups
	MGROUP_SCALE *mgsSX[VesselCollisionDef::MAX_SHAPES];
	MGROUP_SCALE *mgsSY[VesselCollisionDef::MAX_SHAPES];
	MGROUP_SCALE *mgsSZ[VesselCollisionDef::MAX_SHAPES];

	// Scaling animation IDs
	UINT animSX[VesselCollisionDef::MAX_SHAPES];
	UINT animSY[VesselCollisionDef::MAX_SHAPES];
	UINT animSZ[VesselCollisionDef::MAX_SHAPES];

	ANIMATIONCOMPONENT_HANDLE firstAnimHandle, parent;
	UINT mMeshIndices[VesselCollisionDef::MAX_SHAPES];
	static const int GROUP_INDEX = 0;

	//The attached VESSEL's pointer
	OBJHANDLE hObjMesh;



	// Create the animations
	int 	createAnimations(float sx, float sy, float sz);

	// Key commands for translation/rotation/scaling
	int		processCommand();

	int 	addSimpleShape(btCollisionShape* collShape);
	int 	addCompoundShape(btCompoundShape* compoundShape);

	// Not used
	MESHHANDLE createBoxShape(btVector3& halfExtent);
	/* MESHHANDLE createSphereShape(float radius);
	MESHHANDLE createCylinderShape(float radius, float height);*/



public:

	Bump_CollisionShape();
	virtual ~Bump_CollisionShape();

	// Each mesh vessel should have an unique name
	char strMeshVesselName[MAX_STRLEN];
	char strMeshFileName[MAX_STRLEN];


	// Main control functions which are overidden from parent as they are called from Bump_BulletBase
	// This must check if the child offsets have changed
	int		drawSimpleShape(btTransform& shapeTransform);

	// Requires ability to set local transforms of child meshes in MeshProxy vessel
	int		drawCompoundShape(btTransform& transShape, btCompoundShape& compoundShape);

	//This must be called to add the shape, the shape may be a convex hull with associated .msh file
	int		addSimpleShape(Bump_BulletBase *base,
						   btCollisionShape *collShape,
						   char *strMshVesselName,
					  	   char *fileName = NULL);

	//This must be called to add the shape
	int		addCompoundShape(Bump_BulletBase *base,
						     btCollisionShape *collShape,
						     char *strMshVesselName,
						     char *pFileNames[]);

	// Remove the shape - delete vessel, attachment point on base
	int		removeShape(void);

	// Reset Animations
	int 	resetAnimations(void);


};
